﻿using BepInEx.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using Blis.Client;
using Blis.Server;
using Blis.Common;
using HarmonyLib;

namespace BlackSurvivalCheat
{
    static class X_Ray
    {
        static bool maintoggle;
        static List<PatchHandler> handles;


        static X_Ray()
        {
            maintoggle = true;

            handles = new List<PatchHandler>();
            handles.Add(new PatchHandler { Postfix = AccessTools.Method(typeof(X_Ray), "ClientService_StartGame_Postfix", null, null), TargetType = typeof(ClientService), TargetMethonName = "StartGame" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalPlayerCharacter_HideMapIcon_Prefix"), TargetType = typeof(LocalPlayerCharacter), TargetMethonName = "HideMapIcon" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalPlayerCharacter_HideMiniMapIcon_Prefix"), TargetType = typeof(LocalPlayerCharacter), TargetMethonName = "HideMiniMapIcon" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalPlayerCharacter_OnDead_Prefix"), TargetType = typeof(LocalPlayerCharacter), TargetMethonName = "OnDead" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalPlayerCharacter_UpdateMapIconPos_Prefix"), TargetType = typeof(LocalPlayerCharacter), TargetMethonName = "UpdateMapIconPos" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalBotPlayerCharacter_HideMapIcon_Prefix"), TargetType = typeof(LocalBotPlayerCharacter), TargetMethonName = "HideMapIcon" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalBotPlayerCharacter_HideMiniMapIcon_Prefix"), TargetType = typeof(LocalBotPlayerCharacter), TargetMethonName = "HideMiniMapIcon" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalBotPlayerCharacter_OnDead_Prefix"), TargetType = typeof(LocalBotPlayerCharacter), TargetMethonName = "OnDead" });
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(X_Ray), "LocalBotPlayerCharacter_UpdateMapIconPos_Prefix"), TargetType = typeof(LocalBotPlayerCharacter), TargetMethonName = "UpdateMapIconPos" });
        }

        public static void Load(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Patch(harmony);
            }
        }

        public static void UnLoad(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Unpatch(harmony);
            }
        }
        static void ClientService_StartGame_Postfix(ClientService __instance, GameClient ___gameClient, float frameUpdateRate)
        {
            if (!maintoggle)
            {
                return;
            }
            foreach (PlayerContext playerContext in __instance.GetPlayers())
            {
                playerContext.Character.ShowMapIcon(MiniMapIconType.Sight);
                playerContext.Character.ShowMiniMapIcon(MiniMapIconType.Sight);
            }
        }

        static bool LocalPlayerCharacter_HideMapIcon_Prefix(LocalPlayerCharacter __instance, MiniMapIconType miniMapIconType)
        {
            if (!maintoggle)
                return true;
            return false;
        }
        static bool LocalPlayerCharacter_HideMiniMapIcon_Prefix(LocalPlayerCharacter __instance, MiniMapIconType miniMapIconType)
        {
            if (!maintoggle)
                return true;
            return false;
        }
        static bool LocalPlayerCharacter_OnDead_Prefix(LocalPlayerCharacter __instance, LocalCharacter attacker)
        {
            if (!maintoggle)
                return true;
            __instance.ShowMapIcon(MiniMapIconType.Sight);
            __instance.ShowMiniMapIcon(MiniMapIconType.Sight);
            return true;
        }
        static bool LocalPlayerCharacter_UpdateMapIconPos_Prefix(LocalPlayerCharacter __instance)
        {
            if (!maintoggle)
                return true;
            bool isAlly = Blis.Common.Utils.MonoBehaviourInstance<ClientService>.inst.MyPlayer.IsAlly(__instance);
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.CombineWindow.UpdateMapPlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.MapWindow.UpdateMapPlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.Minimap.UIMap.UpdatePlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.HyperloopWindow.UpdateMapPlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            return false;
        }

        static bool LocalBotPlayerCharacter_HideMapIcon_Prefix(LocalBotPlayerCharacter __instance, ref MiniMapIconType miniMapIconType)
        {
            if (!maintoggle)
                return true;
            return false;
        }
        static bool LocalBotPlayerCharacter_HideMiniMapIcon_Prefix(LocalBotPlayerCharacter __instance, ref MiniMapIconType miniMapIconType)
        {
            if (!maintoggle)
                return true;
            return false;
        }
        static bool LocalBotPlayerCharacter_OnDead_Prefix(LocalBotPlayerCharacter __instance, ref LocalCharacter attacker)
        {
            if (!maintoggle)
                return true;
            __instance.ShowMapIcon(MiniMapIconType.Sight);
            __instance.ShowMiniMapIcon(MiniMapIconType.Sight);
            return true;
        }
        static bool LocalBotPlayerCharacter_UpdateMapIconPos_Prefix(LocalBotPlayerCharacter __instance)
        {
            if (!maintoggle)
                return true;
            bool isAlly = Blis.Common.Utils.MonoBehaviourInstance<ClientService>.inst.MyPlayer.IsAlly(__instance);
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.CombineWindow.UpdateMapPlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.MapWindow.UpdateMapPlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.Minimap.UIMap.UpdatePlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            Blis.Common.Utils.MonoBehaviourInstance<GameUI>.inst.HyperloopWindow.UpdateMapPlayerPosition(__instance.ObjectId, isAlly, __instance.GetPosition());
            return false;
        }
    }
}
